package com.xinxing.learning.batch.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xinxing.learning.batch.mapper.origin", sqlSessionFactoryRef = "originSqlSessionFactory")
public class OriginDataSourceConfig {

    @Bean("originDatasource")
    @ConfigurationProperties(prefix = "spring.origin-datasource")
    public DataSource originDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("originSqlSessionFactory")
    @DependsOn("originDatasource")
    public SqlSessionFactory sessionFactory(@Qualifier("originDatasource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage("com.xinxing.learning.batch.entity");
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        String localMapperXmlPattern = "classpath*:/mapper/origin/*.xml";
        mybatisSqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(localMapperXmlPattern));
        MybatisConfiguration configuration = new MybatisConfiguration();
        // configuration.setLogImpl(StdOutImpl.class);
        configuration.setMapUnderscoreToCamelCase(true);
        mybatisSqlSessionFactoryBean.setConfiguration(configuration);
        mybatisSqlSessionFactoryBean.setPlugins(originMybatisPlusInterceptor());
        return mybatisSqlSessionFactoryBean.getObject();
    }

    @Bean("originMybatisPlusInterceptor")
    public MybatisPlusInterceptor originMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        // 版本号实现乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
